package tongcheng.com.tongcheng.base.main;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.adapter.OrderListAdapter;
import tongcheng.com.tongcheng.base.BaseFragment;
import tongcheng.com.tongcheng.bean.OrderListBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;

import static tongcheng.com.tongcheng.ui.yemian.shenfen.activity.InstallPersonCenter.ssInstance;

/**
 * Created by P on 2017/7/12.
 */

public class FragmentOrder extends BaseFragment {
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.order_rv)
    RecyclerView mOrderRv;
    @Bind(R.id.title_bar)
    RelativeLayout mTitleBar;
    @Bind(R.id.line)
    View mLine;


    public List<OrderListBean.DataBean> datas = new ArrayList<>();
    private OrderListAdapter mOrderListAdapter;

    @Override
    public void FragmentStart() {
        initData();
    }

    @Override
    public Integer setFragmentView() {
        return R.layout.fragment_order;
    }

    @Override
    public void FragmentCreate(View inflate) {
        ButterKnife.bind(this, inflate);
        settitlebar(mTitleBar);
        mTitle.setText("订单管理");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mOrderRv.setLayoutManager(linearLayoutManager);
        mOrderListAdapter = new OrderListAdapter(mContext,datas);

        if(ssInstance != null){
            mOrderListAdapter.setaBoolean(true);
        }
        mOrderRv.setAdapter(mOrderListAdapter);
       // mLine.setVisibility(View.INVISIBLE);
        initData();
    }


    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        MyRequest request = new MyRequest(ConStans.OrderList_Url);
        request.add("userid", Setting.getId());
        CallServer.getInstance().addPost(mContext,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("jjjj", json);
                OrderListBean orderListBean = GsonUtil.GsonToBean(json, OrderListBean.class);
                if (orderListBean != null) {
                    List<OrderListBean.DataBean> data = orderListBean.data;
                    datas.clear();
                    datas.addAll(data);
                    if (mOrderListAdapter != null) {
                        mOrderListAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        if(ssInstance != null){
            ssInstance = null;
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }
}
